package org.jeecg.modules.websocket.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.modules.redis.listener.JeecgRedisListener;
import org.jeecg.modules.websocket.manager.WebSocketSessionGroup;
import org.jeecg.modules.websocket.manager.WebSocketSessionManager;
import org.jeecg.modules.websocket.constant.WsMessageConstant;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;

/**
 * @author by sean
 * @desc: 监听来自Redis的消息
 * @since 2024-09-24 星期二 15:04:08
 */
@Slf4j
@Component(WsMessageConstant.EBIKE_REALTIME_MESSAGE)
public class EBikeRealtimeMessageHandler implements JeecgRedisListener {

    @Override
    public void onMessage(BaseMap map) {
        log.debug("【Redis发布订阅模式】redis Listener: {}，参数：{}", WsMessageConstant.EBIKE_REALTIME_MESSAGE, map.toString());
        String terminalCode = map.get("terminalCode");
        String message = map.get("message");
        log.info("给{}分组发送了消息,消息内容:{}", terminalCode, message);
        if (StrUtil.isNotBlank(terminalCode) && StrUtil.isNotBlank(message) ){
            WebSocketSessionGroup sessionGroup = WebSocketSessionManager.get(terminalCode);
            if(ObjectUtil.isNotEmpty(sessionGroup)  && !sessionGroup.isEmpty()){
                sessionGroup.sendMessage(new TextMessage(message));
            }
        }
    }
}
